#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>

/*
pthread_exit()
    在退出线程的时候，很有可能会产生僵尸线程
*/

typedef struct STUDENT {
    char name[523];
    int age;
}Stu;

Stu s;

void* mythread(void* arg) {
    printf("子线程, 其所在进程: pid = [%d], 线程id = [%ld]\n", getpid(), pthread_self());
    
    // int a = 666;
    s.age = 18;    
    strcpy(s.name, "Li4");

    printf("回调函数中: name = %s, age = %d, &s = [%p]\n", s.name, s.age, &s);

    pthread_exit(&s);

    // sleep(10000);
}

int main()
{
    // int pthread_create(pthread_t *thread,  const pthread_attr_t *attr,   void *(*start_routine) (void *),  void *arg);
    pthread_t thr;
    int ret = pthread_create(&thr, NULL, mythread, NULL);
    if(ret != 0) {
        printf("pthread_create err: [%s]\n", strerror(ret));
        return -1;
    }

    printf("主线程, 其所在进程: pid = [%d], 主线程 id = [%ld]\n", getpid(), pthread_self());


    void* p = NULL;
    pthread_join(thr, &p);
    
    Stu* n = (Stu*)p;
    printf("回收子线程: name = %s, age = %d, p = [%p]\n", n->name, n->age, p);

    sleep(1);   // 确保子线程能执行
    return 0;
}

